#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;

// 树状数组不能用来求解最大值，这是由于树状数组范围不够精细，只能用来总体考虑
typedef long long LL;
const int N = 100010;
struct node
{
    int l, r;
    int maxx;
}tree[4 *N];
int w[N];
int n, m;

void push_up(int u)
{
    tree[u].maxx = max(tree[u << 1].maxx, tree[u << 1 | 1].maxx);
}

void build(int u, int l, int r)
{
    if(l == r)
        tree[u] = {l, r, w[l]};
    else
    {
        tree[u].l = l, tree[u].r = r;
        int mid = (l + r) >> 1;
        // 首先求出左右孩子的最大值
        build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
        push_up(u);
    }
}

int query(int u, int l, int r)
{
    if(l <= tree[u].l && r >= tree[u].r)
        return tree[u].maxx;
    else
    {
        int ans = INT_MIN;
        int mid = (tree[u].l + tree[u].r) >> 1;
        if(l <= mid) ans = max(ans, query(u << 1, l, r));
        // 这里直接填else不行，会直接跳过查找右半边
        // 填l > mid也不行，当区间横跨时也不会查找右半边
        if(r > mid)  ans = max(ans, query(u << 1 | 1, l, r));
        return ans;
    }
}

int main()
{
    // ios::sync_with_stdio(false);
    // cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
        scanf("%d", &w[i]);
        // cin >> w[i];
    build(1, 1, n);
    // for(int i = 1; i <= n*4; ++i)
        // cout << i << ' ' << tree[i].l << ' ' << tree[i].r << ' ' << tree[i].maxx << endl;
    while(m--)
    {
        int l, r;
        // cin >> l >> r;
        scanf("%d%d", &l, &r);
        // cout << query(1, l, r) << endl;
        printf("%d\n", query(1, l, r));
    }
    return 0;
}

